package co.yixiang.controller.manager.trace;

import co.yixiang.modules.trace.entity.domain.TraceBreed;
import co.yixiang.modules.trace.service.TraceBreedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "溯源:溯源管理")
@RestController
@RequestMapping("api/traceBreed")
public class TraceBreedController {
    @Autowired
    private TraceBreedService traceBreedService;

    @ApiOperation(value = "分页查询")
    @GetMapping
    @PreAuthorize("hasAnyRole('admin','common','TRACEBREED_ALL','TRACEBREED_SELECT')")
    public ResponseEntity getTraceBreedPage(TraceBreed criteria, Pageable pageable) {
        return new ResponseEntity(traceBreedService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation(value = "查询")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAnyRole('admin','common','TRACEBREED_ALL','TRACEBREED_SELECT')")
    public ResponseEntity getTraceBreedList(TraceBreed criteria) {
        return new ResponseEntity(traceBreedService.queryAll(criteria), HttpStatus.OK);
    }

    @ApiOperation(value = "溯源")
    @GetMapping(value = "/read")
    public ResponseEntity readTraceBreed(TraceBreed criteria) {
        // traceBreedService.findById(criteria);
        return new ResponseEntity(null, HttpStatus.OK);
    }


    @ApiOperation(value = "新增")
    @PostMapping
    @PreAuthorize("hasAnyRole('admin','common','TRACEBREED_ALL','TRACEBREED_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TraceBreed resources) {
        return new ResponseEntity(traceBreedService.create(resources), HttpStatus.CREATED);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    @PreAuthorize("hasAnyRole('admin','common','TRACEBREED_ALL','TRACEBREED_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TraceBreed resources) {
        traceBreedService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyRole('admin','common','TRACEBREED_ALL','TRACEBREED_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        traceBreedService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
